
app.controller('bindBuyerCtrl', ['$scope', function($scope) {

	refreshBuyers();
	
	/* 刷新买号列表 */
	function refreshBuyers() {
		AQ.callAQ("getBuyers.do", null, function(result) {
			var data = [];
			for (var i = 1; i < result.length; ++i) {
				data.push(result[i]);
			}
			$scope.$apply(function () {
				$scope.listData = data;
			});
		}, function() {
			Utils.msgDialog(err + "["+ code +"]");
		});
	}
	
	$scope.deleteBuyer = function(buyerNick) {
		Utils.checkDialog("确定删除买号“"+ buyerNick +"”吗？", "确定", "取消",
			function() {
				var param = {
					buyerNick: buyerNick,
				};
				AQ.callAQ("deleteBuyer.do", param, function(result) {
					refreshBuyers();
				}, function(err, code) {
					Utils.msgDialog(err + "["+ code +"]");
				});
			},
			function() {}
		);
		
	};
	
	$scope.noteBuyer = function(buyerId, note) {
		var content = '添加备注：<input type="text" value="'+note+'" id="buyer-note" style="width:250px; height:35px; border:1px solid #B3B3B3; padding-left:5px;"/>';
		
		Utils.checkDialog(content, "添加", "返回", function() {
			var newNote = $.trim($("#buyer-note").val());
			if(newNote.length > 6) {
				alert("备注最多6个汉字！");
				return;
			}
			var param = {
					buyerId: buyerId,
					note: newNote,
				};
				AQ.callAQ("noteBuyer.do", param, function(result) {
					refreshBuyers();
				}, function(err, code) {
					Utils.msgDialog(err + "["+ code +"]");
				});
			},
			function() {}
		);
		
	};
	
	/* 绑定买号 */
	$(".bd-ww-bt").click(function(e) {
		var nick = $.trim($("#wwNick").val());
		if($.trim(nick).length < 3) {
			Utils.msgDialog("请输入正确的淘宝账号！");
			return;
		}
		$(".bd-ww-bt").val("正在绑定···");
		$(".bd-ww-bt").prop("disabled", true);
		
		var param = {
			buyerNick: nick,
		};
        AQ.callAQ("bindBuyer.do", param, function(result) {
			$(".bd-ww-bt").val("绑定");
			$(".bd-ww-bt").prop("disabled", false);
			Utils.msgDialog(result[0].info);
			refreshBuyers();
		}, function(err, code) {
			$(".bd-ww-bt").val("绑定");
			$(".bd-ww-bt").prop("disabled", false);
			Utils.msgDialog(err + "["+ code +"]");
		});
    });
	
}]);